<?php
/**
 * 功能: 订单冻结金额（每小时执行一次）
 * 上月已结算的佣金，在次月的25号才会变为可提现金额。
 * User: WD
 * Date: 2018/12/3 0003
 * Time: 9:27
 */

namespace app\index\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class Freeze extends Command{
    protected function configure(){
        $this->setName('freeze')->setDescription('订单提现');
    }

    protected function execute(Input $input, Output $output){
        $output->writeln("order:");
        /*用不超时*/
        ini_set('max_execution_time', 0);
        $this->index();
    }
    public function index(){
        $sbegin = date('Y-m-26', strtotime('-1 month'));//上月26号
        //   $sbegin = date('Y-m-1', strtotime('-2 month'));//上月26号
        $order = Db::name('goods_order')
            ->alias('go')
            ->join('user u','u.id = go.user_id')
            ->where('go.state',2)
            ->whereTime('go.finish_time','>=',$sbegin)
            ->field('go.*,u.status')
            ->select();
        /*echo "<pre>";
        print_r($order);
        die;*/

        if($order){
            foreach($order as $k=>$v){
                $t = Db::name('goods_order')
                    ->where('state',2)
                    ->where('user_id',$v['user_id'])
                    ->whereTime('finish_time','>=',$sbegin)
                    ->sum('member_actual');
                /* dump($t);
                 die;*/
                $freeze = $t;
                if($v['status']==3||$v['status']==4){
                    $rule = Db::name('partner_rule')->where('status',$v['status'])->find();
                    $level1 = $rule['level1']/100;
                    $level2 = $rule['level2']/100;
                    $son = Db::name('user')->where('parent_id',$v['user_id'])->select();
                    if($son){
                        $son_ids = [];
                        foreach($son as $k1 => $v1){
                            $son_ids[] = $v1['id'];
                        }
                        $freeze1 = Db::name('goods_order')
                            ->where('state',2)
                            ->whereIn('user_id',$son_ids)
                            ->whereTime('finish_time','>=',$sbegin)
                            ->sum('member_actual');
                        $t1 = round($freeze1*$level1,2);
                        $son2 = Db::name('user')->whereIn('parent_id',$son_ids)->select();
                        if($son2){
                            $son2_ids = [];
                            foreach($son2 as $k2=>$v2){
                                $son2_ids[] = $v2['id'];
                            }
                            $freeze2 =  Db::name('goods_order')
                                ->where('state',2)
                                ->whereIn('user_id',$son2_ids)
                                ->whereTime('finish_time','>=',$sbegin)
                                ->sum('member_actual');
                            $t2 =  round($freeze2*$level2,2);
                        }else{
                            $t2 = 0;
                        }
                        $freeze = round($t+$t1+$t2,2) ;
                    }else{
                        $freeze = $t;
                    }

                }
                /* dump($freeze);
                 die;*/
                Db::startTrans();
                try{
                    Db::name('user')->where('id',$v['user_id'])->update(['freeze'=>$freeze]);
                    // 提交事务
                    Db::commit();
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                }
            }
        }


    }

}